package com.itheima.controller.store;

import com.github.pagehelper.PageInfo;
import com.itheima.controller.BaseServlet;
import com.itheima.domain.store.Member;
import com.itheima.domain.store.Orders;
import com.itheima.domain.store.Product;
import com.itheima.service.impl.store.OrdersServiceImpl;
import com.itheima.service.store.OrdersService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/store/orders/*")
public class OrdersServlet extends BaseServlet {

    private OrdersService ordersService=new OrdersServiceImpl();
    //遍历
    protected void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int pageNum = 1;
        int pageSize = 3;
        if (req.getParameter("pageNum") != null)
            pageNum = Integer.parseInt(req.getParameter("pageNum"));
        if (req.getParameter("pageSize") != null)
            pageSize = Integer.parseInt(req.getParameter("pageSize"));
        List<Orders> list=ordersService.list(pageNum,pageSize);
        PageInfo pageInfo=new PageInfo(list);
        req.setAttribute("pageInfo",pageInfo);
        req.getRequestDispatcher("/WEB-INF/pages/store/orders/orders-list.jsp").forward(req,resp);
    }

    //跳转show回显
    protected void toShow (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取orders的id
        String id = req.getParameter("id");
        //根据id查orders
        Orders orders= ordersService.findById(id);
        req.setAttribute("orders",orders);
        req.getRequestDispatcher("/WEB-INF/pages/store/orders/orders-show.jsp").forward(req,resp);
    }

    //删除
    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String ids = req.getParameter("ids");
        String[] split = ids.split(",");
        for (int i = 0; i < split.length; i++) {
            ordersService.deleteById(split[i]);
        }
        list(req, resp);
    }

    private String msg;

    //模糊查询
    protected void selectLike(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int pageNum = 1;
        int pageSize = 3;
        if (req.getParameter("pageNum") != null){
            pageNum = Integer.parseInt(req.getParameter("pageNum"));}
        if (req.getParameter("pageSize") != null){
            pageSize = Integer.parseInt(req.getParameter("pageSize"));}
        if (req.getParameter("msg")!=null){
            msg = req.getParameter("msg");
        }
        String lms="%"+msg+"%";
        List<Orders> ordersList= ordersService.findByLike(lms,pageNum,pageSize);
        PageInfo pageInfo=new PageInfo(ordersList);
        req.setAttribute("pageInfo",pageInfo);
        req.getRequestDispatcher("/WEB-INF/pages/store/orders/orders-list.jsp?pageNum="+pageNum).forward(req,resp);
    }
}
